//
//  Scene.cpp
//  archer
//
//  Created by Wu Wang on 12-5-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include "Scene.h"
#include "GameGlobal.h"
#include "CocosHelper.h"
#include <cmath>
using namespace cocos2d;

CCPoint CScene::GetTouchInMain(cocos2d::CCTouch *pTouch)
{
	return ccpMult(m_pLayScene[LAYER_MAIN]->convertToNodeSpace(convertToWorldSpace(convertTouchToNodeSpace(pTouch))), 1/m_mainScale);
}

CCPoint CScene::GetPosInMain(cocos2d::CCPoint pt)
{
	return m_pLayScene[LAYER_MAIN]->convertToNodeSpace(pt);
}


void CScene::InitSceneBase(const char *szGroundPath, float groundTileWidth, const char *szBackPath, float backTileWidth, const char *szTablePath)
{
	for(int i=0; i<LAYER_MAX; i++)
	{
		m_pLayScene[i] = CCLayer::node();
		addChild(m_pLayScene[i], i);
	}

	float groundHeight = 120.0f;
	float resScale = CGameGlobal::GetInstance()->m_resScale;
	CCSize sScreen = CCDirector::sharedDirector()->getWinSize();
	ccColor4B black = {0, 0, 0, 255};
	CCLayerColor *pLayColor = CCLayerColor::layerWithColor(black, WORLD_WIDTH*resScale * 1.5f, 800 *resScale);
	pLayColor->setIsRelativeAnchorPoint(true);
	pLayColor->setAnchorPoint(ccp(0, 1));
	pLayColor->setPosition(ccp(0, groundHeight*resScale));
	m_pLayScene[LAYER_MAIN]->addChild(pLayColor, 100);

	CCSpriteBatchNode *pBatchBack = CCSpriteBatchNode::batchNodeWithFile(szBackPath);
	m_pLayScene[LAYER_BACK]->addChild(pBatchBack);
	CCSprite *pSprBack;
	for(int i=0; i<((int)SCREEN_WIDTH/backTileWidth) + 1; i++)
	{
		pSprBack = CCSprite::spriteWithFile(szBackPath);
		pSprBack->setAnchorPoint(ccp(0, 0));
		pSprBack->setPosition(ccp(i * backTileWidth * resScale , 0));
		pBatchBack->addChild(pSprBack);
		pSprBack->getTexture()->setAliasTexParameters();
	}

	CCSpriteBatchNode *pBatchGround = CCSpriteBatchNode::batchNodeWithFile(szGroundPath);
	m_pLayScene[LAYER_MAIN]->addChild(pBatchGround, ZORDER_GROUND);
	CCSprite *pSprGround;
	for(int i=0; i<WORLD_WIDTH / groundTileWidth + 1; i++)
	{
		pSprGround = CCSprite::spriteWithFile(szGroundPath);
		pSprGround->setAnchorPoint(ccp(0, 0));
		pSprGround->getTexture()->setAliasTexParameters();
		pSprGround->setPosition(ccpAdjustRes(groundTileWidth*i, groundHeight));
		pBatchGround->addChild(pSprGround);
	}

	m_pLayScene[LAYER_MAIN]->setAnchorPoint(ccp(0, groundHeight/SCREEN_HEIGHT));
	m_pLayScene[LAYER_MIDDLE]->setAnchorPoint(ccp(0, groundHeight/SCREEN_HEIGHT));
	m_pLayScene[LAYER_BACKOBJ]->setAnchorPoint(ccp(0, groundHeight/SCREEN_HEIGHT));

	m_ptOrgin = ccp(0, 0);
	m_ptOrginDist = m_ptOrgin;
	m_mainScaleDist = m_mainScale = 1.0f;


}

void CScene::MoveMainScene(float xAdd, float yAdd)
{
	float screenWidth = CCDirector::sharedDirector()->getWinSize().width * CGameGlobal::GetInstance()->m_resolutionScale;

	if(m_ptOrginDist.x + screenWidth + xAdd <= WORLD_WIDTH * m_mainScale && m_ptOrginDist.x + xAdd >= 0)
	{
		m_ptOrginDist.x+=xAdd;
	}

	if(m_ptOrginDist.y + SCREEN_HEIGHT + yAdd <= WORLD_HEIGHT * m_mainScale && m_ptOrginDist.y + yAdd >= 0)
	{
		m_ptOrginDist.y += yAdd;
	}

	m_ptOrgin = m_ptOrginDist;
}

void CScene::ScaleMainScene(float dist)
{
	if(fabsf(dist) < 2)
	{
		return;
	}

	float screenWidth = CCDirector::sharedDirector()->getWinSize().width;
	float scaleDiff = 0.5f * dist / screenWidth;
	float newScale  = m_mainScale + scaleDiff;


	//newScale += scaleDiff;

	float minScale = CCDirector::sharedDirector()->getWinSize().width / CGameGlobal::GetInstance()->m_resScale / WORLD_WIDTH;
	if(newScale < minScale)
	{
		newScale = minScale;
	}
	else if(newScale > 1.0f)
	{
		newScale = 1.0f;
	}

	scaleDiff = newScale - m_mainScale;

    float test = fabsf(scaleDiff);
	if(test < 0.05f)
	{
		return;
	}

    ScaleMainWithCenter(m_ptScaleCenter, newScale, 2);
}
//-------------------------------------------------------------------------------------------------------------/
void CScene::ScaleMainWithCenter(cocos2d::CCPoint ptCenter, float curScale, float scaleSpeed)
{
	m_mainScaleDist = curScale;
	m_scaleSpeed    = scaleSpeed;

	CCSize sScreen = CCDirector::sharedDirector()->getWinSize();
	float resScale = CGameGlobal::GetInstance()->m_resScale;

	sScreen.width  /= (resScale);
	sScreen.height /= (resScale);

	m_ptOrginDist.x = ptCenter.x - sScreen.width / 2;
	m_ptOrginDist.y = ptCenter.y - sScreen.height / 2;


	m_ptOrginDist = ccpMult(m_ptOrginDist, m_mainScaleDist);

	if(m_ptOrginDist.x > WORLD_WIDTH * m_mainScaleDist- sScreen.width)
	{
		m_ptOrginDist.x = WORLD_WIDTH * m_mainScaleDist- sScreen.width;
	}
	else if(m_ptOrginDist.x < 0)
	{
		m_ptOrginDist.x = 0;
	}

	if(m_ptOrginDist.y > WORLD_HEIGHT * m_mainScaleDist- sScreen.height)
	{
		m_ptOrginDist.y = WORLD_HEIGHT * m_mainScaleDist- sScreen.height;
	}
	else if(m_ptOrginDist.y < 0)
	{
		m_ptOrginDist.y = 0;
	}

	m_bStep = true;
}

//-----------------------------------------------------------------------------------------------------------------------------------------/
void CScene::SetMainScale(float distScale, float duration)
{
	m_mainScaleDist = distScale;
	m_scaleSpeed    = duration * 60;
	m_bStep = false;
}

//-----------------------------------------------------------------------------------------------------------------------------------------/
void CScene::SetMainMoveTo(cocos2d::CCPoint ptDist, float duration)
{

	m_scaleSpeed    = duration * 60;
	CCSize sScreen = CCDirector::sharedDirector()->getWinSize();
	float resScale = CGameGlobal::GetInstance()->m_resScale;

	sScreen.width  /= (resScale);
	sScreen.height /= (resScale);

	m_ptOrginDist.x = ptDist.x - sScreen.width / 2;
	m_ptOrginDist.y = ptDist.y - sScreen.height / 2;


	m_ptOrginDist = ccpMult(m_ptOrginDist, m_mainScaleDist);

	if(m_ptOrginDist.x > WORLD_WIDTH * m_mainScaleDist- sScreen.width)
	{
		m_ptOrginDist.x = WORLD_WIDTH * m_mainScaleDist- sScreen.width;
	}
	else if(m_ptOrginDist.x < 0)
	{
		m_ptOrginDist.x = 0;
	}

	if(m_ptOrginDist.y > WORLD_HEIGHT * m_mainScaleDist- sScreen.height)
	{
		m_ptOrginDist.y = WORLD_HEIGHT * m_mainScaleDist- sScreen.height;
	}
	else if(m_ptOrginDist.y < 0)
	{
		m_ptOrginDist.y = 0;
	}

	m_bStep = false;
}
//-----------------------------------------------------------------------------------------------------------------------------------------/
bool CScene::UpdateScale(cocos2d::ccTime dt)
{
    bool bRet = false;
    static bool bFirst = true;
    CCSize s = CCDirector::sharedDirector()->getWinSize();
    
    static CCPoint ptLast = m_ptOrgin;
    
    static float xStep = 10;
    static float yStep = 10;
    static float scaleStep = 0.01f;
    
    if(bFirst)
    {
        ptLast = m_ptOrgin;
        bFirst = false;
        xStep = fabsf(ptLast.x - m_ptOrginDist.x) / m_scaleSpeed;
        yStep = fabsf(ptLast.y - m_ptOrginDist.y) / m_scaleSpeed;
        scaleStep = fabsf(m_mainScaleDist - m_mainScale) / m_scaleSpeed;
    }
    
    if(CalStep(&m_mainScale, m_mainScaleDist, scaleStep))
    {
        bRet = true;
    }
    
    if(CalStep(&m_ptOrgin.x, m_ptOrginDist.x, xStep))
    {
        bRet = true;
    }
    
    if(CalStep(&m_ptOrgin.y, m_ptOrginDist.y, yStep))
    {
        bRet = true;
    }
    
    if(!bRet)
    {
        bFirst = true;
    }
    
    // 更新缩放

	if(!m_bStep)
	{
		return false;
	}
    return bRet;
    
}

void CScene::FollowObject(cocos2d::CCPoint pt)
{

    pt = ccpMult(pt, m_mainScale);
	float pw = CCDirector::sharedDirector()->getWinSize().width * CGameGlobal::GetInstance()->m_resolutionScale;
	float diff = pt.x - m_ptOrgin.x;
    pw *= m_mainScale;
	if(diff > pw*0.5f && pt.x < WORLD_WIDTH*m_mainScale - pw*0.5f)
	{
		m_ptOrgin.x += diff - pw*0.5f;
	}
	else if(diff < pw*0.5f && pt.x > pw*0.5f)
	{
		m_ptOrgin.x += diff - pw*0.5f;
	}

	m_ptOrginDist = m_ptOrgin;
}

bool CScene::Shake(float dt, float duration, int time)
{

	static CCPoint ptOrgLast = m_ptOrgin;
	static int   leftTimes = 10;
	static float s = 1;
	static float localTimer = 0;

	if(10 == leftTimes)
	{
		ptOrgLast = m_ptOrgin;
	}

	localTimer += dt;

	if(localTimer > 0.02f)
	{
		localTimer = 0;
		s *= -1;

		m_ptOrginDist.x = m_ptOrgin.x = ptOrgLast.x + leftTimes * m_mainScale * s * 0.3;
		m_ptOrginDist.y = m_ptOrgin.y = ptOrgLast.y + leftTimes * m_mainScale * s * 0.3;
		leftTimes--;
	}

	if(leftTimes <= 0)
	{
		leftTimes = 10;

		return false;
	}

	return true;
}

void CScene::LocateCurOrgin()
{
	m_ptOrgin = m_ptOrginDist;
}
//-----------------------------------------------------------------------------------------------------------------------------------------/
bool CScene::CalStep(float *org, float dist, float step)
{
	float diff = dist - *org;

	if(fabsf(diff) > step)
	{
		*org += step * diff/fabsf(diff);
		return true;
	}
	else
	{
		*org = dist;
		return false;
	}
}

void CScene::updateScene(cocos2d::ccTime dt)
{
	m_pLayScene[LAYER_MAIN]->setPosition(ccp(-m_ptOrgin.x * CGameGlobal::GetInstance()->m_resScale, 
		-m_ptOrgin.y * CGameGlobal::GetInstance()->m_resScale));

	m_pLayScene[LAYER_MAIN]->setScale(m_mainScale);

	m_pLayScene[LAYER_MIDDLE]->setPosition(ccp(-m_ptOrgin.x * CGameGlobal::GetInstance()->m_resScale * 0.66, 
		-m_ptOrgin.y * CGameGlobal::GetInstance()->m_resScale * 0.66));

	m_pLayScene[LAYER_MIDDLE]->setScale(0.5+ (m_mainScale - 0.5) * 0.66f);

	m_pLayScene[LAYER_BACKOBJ]->setPosition(ccp(-m_ptOrgin.x * CGameGlobal::GetInstance()->m_resScale * 0.15, 
		-m_ptOrgin.y * CGameGlobal::GetInstance()->m_resScale * 0.15));

	m_pLayScene[LAYER_BACKOBJ]->setScale(1.0f + (m_mainScale - 0.5) * 0.33f);

	m_pSprTable[0]->setPosition(ccp(m_pSprTable[0]->getPosition().x, CGameGlobal::GetInstance()->m_resScale * (m_leftTableHeight + m_tableSpace)));
	m_pSprTable[1]->setPosition(ccp(m_pSprTable[1]->getPosition().x, CGameGlobal::GetInstance()->m_resScale * (m_rightTableHeight + m_tableSpace)));
}

bool CScene::moveTable(cocos2d::ccTime dt)
{
	int leftDiff = m_leftTableHeightDist - m_leftTableHeight;

	static bool bMovingLeft = false;
	static bool bMovingRight = false;

	bool bLeftFinish = false;
	bool bRightFinish = false;

	int leftDiffAbs = abs(leftDiff);
	if(leftDiffAbs > 1.0f)
	{
		leftDiff = leftDiff / leftDiffAbs;

		if(!bMovingLeft)
		{
			bMovingLeft = true;
			float angle = 5.0f;

			if(leftDiff < 0)
			{
				angle = -5.0f;
			}
		}
	}
	else
	{
		leftDiff = 0;
		m_leftTableHeight = m_leftTableHeightDist;
		bLeftFinish = true;
	}

	m_leftTableHeight += leftDiff;

	int rightDiff = m_rightTableHeightDist - m_rightTableHeight;

	int rightDiffAbs = abs(rightDiff);
	if(rightDiffAbs> 1.0f)
	{
		rightDiff =  rightDiff / rightDiffAbs;

		if(!bMovingRight)
		{
			bMovingRight = true;
			float angle = 5.0f;

			if(rightDiff < 0)
			{
				angle = -5.0f;
			}
			
		}
	}
	else
	{
		rightDiff = 0;
		m_rightTableHeight = m_rightTableHeightDist;
		bRightFinish = true;
		
	}

	m_rightTableHeight += rightDiff;

	if(bLeftFinish && bRightFinish)
	{
		bMovingLeft = bMovingRight = false;
		return false;
	}

	return true;
}

bool CScene1::moveTable(cocos2d::ccTime dt)
{
	int leftDiff = m_leftTableHeightDist - m_leftTableHeight;

	static bool bMovingLeft = false;
	static bool bMovingRight = false;

	bool bLeftFinish = false;
	bool bRightFinish = false;

	int leftDiffAbs = abs(leftDiff);
	if(leftDiffAbs > 1.0f)
	{
		leftDiff =  leftDiff / leftDiffAbs;

		if(!bMovingLeft)
		{
			bMovingLeft = true;
			float angle = 5.0f;

			if(leftDiff < 0)
			{
				angle = -5.0f;
			}
			m_pSprWheel[0]->runAction(CCRepeatForever::actionWithAction(CCRotateBy::actionWithDuration(0.01f, angle)));
		}
	}
	else
	{
		leftDiff = 0;
		m_leftTableHeight = m_leftTableHeightDist;
		bLeftFinish = true;
		m_pSprWheel[0]->stopAllActions();
	}

	m_leftTableHeight += leftDiff;

	int rightDiff = m_rightTableHeightDist - m_rightTableHeight;

	int rightDiffAbs = abs(rightDiff);
	if(rightDiffAbs> 1.0f)
	{
		rightDiff = rightDiff / rightDiffAbs;

		if(!bMovingRight)
		{
			bMovingRight = true;
			float angle = 5.0f;

			if(rightDiff < 0)
			{
				angle = -5.0f;
			}
			m_pSprWheel[1]->runAction(CCRepeatForever::actionWithAction(CCRotateBy::actionWithDuration(0.01f, angle)));
		}
	}
	else
	{
		rightDiff = 0;
		m_rightTableHeight = m_rightTableHeightDist;
		bRightFinish = true;
		m_pSprWheel[1]->stopAllActions();
	}

	m_rightTableHeight += rightDiff;

	if(bLeftFinish && bRightFinish)
	{
		bMovingLeft = bMovingRight = false;
		return false;
	}

	return true;
}
void CScene1::initScene()
{
	m_leftTableHeight = 400;
	m_rightTableHeight = 400;
	int startHeight[2];
	startHeight[0] = m_leftTableHeight;
	startHeight[1] = m_rightTableHeight;

	m_tableSpace    = 15;

	m_tableLeftMin = 450-160;
	m_tableLeftMax = 450+160;
	m_tableRightMin = WORLD_WIDTH - 450 - 160;
	m_tableRightMax = WORLD_WIDTH - 450 + 160;


	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Scenes/Scene1/scene1.plist");

	InitSceneBase("Scenes/Scene1/scene1Ground.png", 165, "Scenes/Scene1/scene1Back.png", 22, NULL);

	CCSpriteBatchNode *pBatchMain = CCSpriteBatchNode::batchNodeWithFile("Scenes/Scene1/scene1.png");
	CCSpriteBatchNode *pBatchMiddle = CCSpriteBatchNode::batchNodeWithFile("Scenes/Scene1/scene1.png");
	m_pLayScene[LAYER_MAIN]->addChild(pBatchMain);
	m_pLayScene[LAYER_MIDDLE]->addChild(pBatchMiddle);
	CCSprite *pSprBackObj = CCSprite::spriteWithSpriteFrameName("scene1BackObj.png");
	pSprBackObj->setAnchorPoint(ccp(0, 0));
	pSprBackObj->setPosition(ccpAdjustRes(0, 100));
	m_pLayScene[LAYER_BACKOBJ]->addChild(pSprBackObj, ZORDER_BACK);

	float startFloorY = 120 + 15;
	int s = 1;
	for(int i=0; i<2; i++)
	{
		CCSprite *pSprFloor = CCSprite::spriteWithSpriteFrameName("scene1Floor.png");
		pSprFloor->setAnchorPoint(ccp(1, 0));
		pSprFloor->setPosition(ccpAdjustRes(WORLD_WIDTH*0.5f - i, startFloorY));
		pBatchMain->addChild(pSprFloor, ZORDER_GROUND);
		pSprFloor->getTexture()->setAntiAliasTexParameters();
		pSprFloor->getTexture()->setAliasTexParameters();



		if(i == 1)
		{
			pSprFloor->setFlipX(true);
			pSprFloor->setAnchorPoint(ccp(0, 0));
		}


		s *= -1;
		m_pSprTable[i] = CCSprite::spriteWithSpriteFrameName("scene1Table.png");
		pBatchMain->addChild(m_pSprTable[i], ZORDER_OBJECTS-4);
		m_pSprTable[i]->setPosition(ccpAdjustRes(WORLD_WIDTH*i - 450 * s, startHeight[i] + m_tableSpace));
		m_pSprTable[i]->setAnchorPoint(ccp(0.5, 1));

		CCSprite *pSprEvel = CCSprite::spriteWithSpriteFrameName("scene1Elevator.png");
		pSprEvel->setAnchorPoint(ccp(0.5, 0));
		pBatchMain->addChild(pSprEvel, ZORDER_OBJECTS-5);
		pSprEvel->setPosition(ccpAdjustRes(WORLD_WIDTH*i - 450 * s, startFloorY));

		CCSprite *pSprPillar = CCSprite::spriteWithSpriteFrameName("scene1PillarFront.png");
		pBatchMain->addChild(pSprPillar, ZORDER_OBJECTS-6);
		pSprPillar->setAnchorPoint(ccp(0.5, 0));
		pSprPillar->setPosition(ccpAdjustRes(WORLD_WIDTH * 0.5f - 400 * s, startFloorY + 100));

		CCSprite *pSprFire = CCSprite::spriteWithSpriteFrameName("scene1fireFront1.png");
		pBatchMain->addChild(pSprFire,ZORDER_OBJECTS-5);
		pSprFire->setPosition(ccpAdjustRes(WORLD_WIDTH * 0.5f - 400 * s, startFloorY + 337 + 100));
		CAnimationHelper::Animate(pSprFire, 1, 3, "scene1fireFront%d.png", 0.1f);
		pSprFire->setAnchorPoint(ccp(0.5f, 0));

		CCSprite *pSprPillarBack = CCSprite::spriteWithSpriteFrameName("scene1PillarBack.png");
		pBatchMiddle->addChild(pSprPillarBack, ZORDER_BACK-1);
		pSprPillarBack->setAnchorPoint(ccp(0.5, 0));
		pSprPillarBack->setPosition(ccpAdjustRes(WORLD_WIDTH * 0.5f - 200 * s, startFloorY + 150));

		CCSprite *pSprFireBack = CCSprite::spriteWithSpriteFrameName("scene1fireFront1.png");
		pBatchMiddle->addChild(pSprFireBack,ZORDER_BACK-1);
		pSprFireBack->setScale(0.7f);
		pSprFireBack->setPosition(ccpAdjustRes(WORLD_WIDTH * 0.5f - 200 * s, startFloorY + 237 + 150));
		CAnimationHelper::Animate(pSprFireBack, 1, 3, "scene1fireFront%d.png", 0.1f);
		pSprFireBack->setAnchorPoint(ccp(0.5f, 0));


		m_pSprWheel[i] = CCSprite::spriteWithSpriteFrameName("scene1Wheel.png");
		m_pSprTable[i]->addChild(m_pSprWheel[i],  -1);
		m_pSprWheel[i]->setPosition(ccpAdjustRes(176, 29));

		CCSprite *pSprDoor = CCSprite::spriteWithSpriteFrameName("scene1Door.png");
		pSprDoor->setAnchorPoint(ccp(0, 0));
		pSprDoor->setPosition(ccpAdjustRes(WORLD_WIDTH*0.5f + 463 - i*(463*2+594), startFloorY));
		pBatchMiddle->addChild(pSprDoor, ZORDER_BACK);
		pSprDoor->getTexture()->setAntiAliasTexParameters();
		pSprDoor->getTexture()->setAliasTexParameters();

		CCSprite *pSprWall = CCSprite::spriteWithSpriteFrameName("scene1Wall.png");
		pSprWall->setAnchorPoint(ccp(0, 0));
		pSprWall->setPosition(ccpAdjustRes(WORLD_WIDTH * 0.5f - 463*i, startFloorY));
		pBatchMiddle->addChild(pSprWall, ZORDER_BACK);
		pSprWall->getTexture()->setAntiAliasTexParameters();
		pSprWall->getTexture()->setAliasTexParameters();
	}

	for(int i=0; i<4; i++)
	{
		CCSprite *pSprFlag = CCSprite::spriteWithSpriteFrameName("scene1Flag.png");
		pSprFlag->setAnchorPoint(ccp(0.5, 0));
		pSprFlag->setPosition(ccpAdjustRes(( i+ 1) * WORLD_WIDTH / 5.0f , startFloorY + 50));
		pBatchMiddle->addChild(pSprFlag, ZORDER_BACK);

	}

	for(int i=0; i< 6; i++)
	{
		if(i % 2 == 0)
		{
			m_pSprCloud[i] = CCSprite::spriteWithSpriteFrameName("scene1Cloud1.png");
		}
		else
		{
			m_pSprCloud[i] = CCSprite::spriteWithSpriteFrameName("scene1Cloud2.png");

		}

		m_pSprCloud[i]->setPosition(ccpAdjustRes(rand() % (int)WORLD_WIDTH, WORLD_HEIGHT* 0.6 - 60 * i - rand() % 30));
		pBatchMiddle->addChild(m_pSprCloud[i], ZORDER_BACK - 5);
		m_pSprCloud[i]->setScale(1.0f - 0.15f * (i / 2));
	}

	SetTableDist(m_leftTableHeight, m_rightTableHeight);
}

void CScene1::updateScene(cocos2d::ccTime dt)
{
	CScene::updateScene(dt);
	UpdateClouds(dt);
}

void CScene1::UpdateClouds(cocos2d::ccTime dt)
{
	float speed;

	for(int i=0; i<6; i++)
	{
		speed = -(6-i) * 0.3f;

		CCPoint ptCloud = CGameGlobal::RestoreResToLogicPos(m_pSprCloud[i]->getPosition());

		ptCloud.x += speed;
		if(ptCloud.x < -200)
		{
			ptCloud.x = WORLD_WIDTH + 200;
		}

		m_pSprCloud[i]->setPosition(CGameGlobal::AdjustResPos(ptCloud));

	}
}

void CScene2::initScene()
{
	CCSprite *pSprBack = CCSprite::spriteWithFile("Scenes/scene2Back.png");
	pSprBack->setPosition(ccp(0, 0));
	pSprBack->setAnchorPoint(ccp(0, 0));
	m_pLayScene[LAYER_BACK]->addChild(pSprBack);

	CCSprite *pSprFront = CCSprite::spriteWithFile("Scenes/scene2Front.png");
	pSprFront->setPosition(ccp(0, 0));
	pSprFront->setAnchorPoint(ccp(0, 0));
	m_pLayScene[LAYER_MAIN]->addChild(pSprFront);

	CCSprite *pSprGround;
	for(int i=0; i<WORLD_WIDTH / 468.0f + 1; i++)
	{
		pSprGround = CCSprite::spriteWithSpriteFrameName("scene2Ground.png");
		pSprGround->setAnchorPoint(ccp(0, 0));
		pSprGround->getTexture()->setAliasTexParameters();
		m_pLayScene[LAYER_MAIN]->addChild(pSprGround, ZORDER_GROUND);
		pSprGround->setPosition(ccpAdjustRes(468*i, 0));
	}

	CCSprite * pSprFloor = CCSprite::spriteWithSpriteFrameName("scene2Floor2.png");
	pSprFloor->setAnchorPoint(ccp(0,0));
	pSprFloor->getTexture()->setAliasTexParameters();
	m_pLayScene[LAYER_MAIN]->addChild(pSprFloor, ZORDER_OBJECTS);
	pSprFloor->setPosition(ccpAdjustRes(0, 100));

	for(int i=0; i<(WORLD_WIDTH-151)/122.0f + 1; i++)
	{
		pSprFloor = CCSprite::spriteWithSpriteFrameName("scene2Floor1.png");
		pSprFloor->setAnchorPoint(ccp(0,0));
		pSprFloor->getTexture()->setAliasTexParameters();
		m_pLayScene[LAYER_MAIN]->addChild(pSprFloor, ZORDER_OBJECTS);
		pSprFloor->setPosition(ccpAdjustRes(151 + 122 * i, 100));
	}

	CCSprite *pSprCenter = CCSprite::spriteWithSpriteFrameName("scene2Center.png");
	pSprCenter->setAnchorPoint(ccp(0.5, 0));
	m_pLayScene[LAYER_MAIN]->addChild(pSprCenter, ZORDER_OBJECTS-5);
	pSprCenter->setPosition(ccpAdjustRes(WORLD_WIDTH*0.5f, 200));


	m_leftTableHeight = 200;
	m_rightTableHeight = 200;
	float startHeight[2];
	startHeight[0] = m_leftTableHeight;
	startHeight[1] = m_rightTableHeight;

	m_tableSpace    = 24;

	int s = 1;
	for(int i=0; i<2; i++)
	{
		s *= -1;
		m_pSprTable[i] = CCSprite::spriteWithSpriteFrameName("scene2Table.png");
		m_pSprTable[i]->setAnchorPoint(ccp(0.5, 0));
		m_pLayScene[LAYER_MAIN]->addChild(m_pSprTable[i], ZORDER_OBJECTS-4);
		m_pSprTable[i]->setPosition(ccpAdjustRes(WORLD_WIDTH*i - 350 * s, startHeight[i] + m_tableSpace));
		m_pSprTable[i]->setAnchorPoint(ccp(0.5, 1));

		CCSprite *pSprFire = CCSprite::spriteWithSpriteFrameName("scene2Fire.png");
		pSprFire->setAnchorPoint(ccp(0.5, 0));
		m_pLayScene[LAYER_MAIN]->addChild(pSprFire, ZORDER_OBJECTS-3);
		pSprFire->setPosition(ccpAdjustRes(WORLD_WIDTH*i - 620 * s, 200));

		CCSprite *pSprFlag = CCSprite::spriteWithSpriteFrameName("scene2Flag.png");
		pSprFlag->setAnchorPoint(ccp(0.5, 0));
		m_pLayScene[LAYER_MAIN]->addChild(pSprFlag, ZORDER_OBJECTS-2);
		pSprFlag->setPosition(ccpAdjustRes(WORLD_WIDTH*i - 100 * s, 200));
	}

	m_tableLeftMin = 350-200;
	m_tableLeftMax = 350+200;
	m_tableRightMin = WORLD_WIDTH - 350 - 200;
	m_tableRightMax = WORLD_WIDTH - 350 + 200;


	m_pLayScene[LAYER_MAIN]->setAnchorPoint(ccp(0, 300 / SCREEN_HEIGHT));
}
CScene *CSceneManager::CreateScene(int id)
{
	return CScene1::node();
}
